<template>
  <div class="content">
    <!-- 顶部的搜索栏 -->
    <div class="input">
      <div class="input-left">
        <span>商品分类：</span>
        <el-cascader
          v-model="value"
          :options="options"
          @change="handleChange"
        ></el-cascader>
      </div>
      <div class="input-right">
        <span>商品搜索：</span>
        <el-input
          placeholder="请输入商品名称，关键字，商品ID"
          v-model="input1"
          size="medium"
          class="inline-input"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click.native="search(input1)"
          ></el-button>
        </el-input>
      </div>
    </div>
    <div class="btn">
      <el-button type="primary" size="small">添加商品</el-button>
      <el-button type="success" size="small">商品采集</el-button>
      <el-button icon="el-icon-upload2" size="small">导出</el-button>
    </div>
    <div class="table">
      <slot></slot>
    </div>
  </div>
</template>

<script>
// 导入axios请求
import { product_list } from "@/network/api/store/index";
import { treeList } from "@/network/api/store/sort";
export default {
  data() {
    return {
      state1: "",
      input1: "",
      value: [],
      options: [],
    };
  },
  methods: {
    selectChanged(val) {},
    async search(value) {
      let { num } = this.$props;
      let res = await product_list(1, 20, num, value);
      // console.log(res.data.data.list);

      res.data.data.list.forEach((item) => {
        let timestamp = item.addTime;

        let newDate = new Date();

        newDate.setTime(timestamp * 1000);
        item.addTime = newDate.toLocaleDateString();
      });
      let list = res.data.data.list;
      this.$emit("send", list, num);
    },
    async handleChange(value) {
      let { num } = this.$props;
      let res = await product_list(1, 20, num, "", value[1]);
      res.data.data.list.forEach((item) => {
        let timestamp = item.addTime;

        let newDate = new Date();

        newDate.setTime(timestamp * 1000);
        item.addTime = newDate.toLocaleDateString();
      });
      let list = res.data.data.list;
      this.$emit("send", list, num);
    },
    dataMethods(value) {
      let arr = [];
      value.forEach((item) => {
        let obj = {
          value: item.id,
          label: item.name,
        };
        if (item.child && item.child.length != 0) {
          obj.children = this.dataMethods(item.child);
        }
        arr.push(obj);
      });
      return arr;
    },
  },
  async created() {
    let list = await treeList(-1);
    // console.log("llllst", list.data.data);
    let options = this.dataMethods(list.data.data);
    this.options = [...options];
  },
  props: {
    num: Number,
  },
};
</script>

<style scoped lang="scss">
.content {
  margin-top: 10px;
  .inline-input {
    width: 300px;
  }
  .top {
    padding: 20px;
  }
  //   input框样式
  .input {
    display: flex;
    justify-content: flex-start;
    .input-left,
    .input-right {
      display: flex;
      width: 500px;
      align-items: center;
      span {
        display: inline-block;
        font-size: 14px;
        color: #606266;
        width: 82px !important;
        margin-bottom: 6px;
        font-weight: 700;
      }
    }
    .input-left {
      margin-right: 20px;
    }
  }

  //   按钮样式
  .btn {
    margin-top: 15px;
    display: flex;

    el-button {
      margin-right: 25px;
    }
  }
  ::v-deep .el-input-group__append {
    position: relative;
    top: -2px;
  }
}
</style>
